//
//  HTToolBarView.swift
//  表情键盘
//
//  Created by whtapple on 4/2/16.
//  Copyright © 2016 whtapple. All rights reserved.
//

import UIKit
//创建协议
protocol HTToolBarViewDelegate: NSObjectProtocol{
    //创建代理方法
    func toolBar(toolBar: HTToolBarView, selectedType: EmotiontoolBarButtonType)
}
enum EmotiontoolBarButtonType: Int{
    case Recent = 0      //最近
    case Default = 1     //默认
    case Emoji  = 2      //emoji
    case Lxh = 3         //浪小花
}

class HTToolBarView: UIView {
    //MARK -属性
    weak var delegate: HTToolBarViewDelegate?
    private var selectedBtn: UIButton?
//      构造函数,子类实现构造函数就不会继承父类的构造函数,如果只实现init(frame:)构造函数,意味着只能使用代码来创建控件,但是UI控件是既能通过代码,也能通过sb/xib来加载
    override init(frame: CGRect) {
        super.init(frame: frame)
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
   }
    class func toolBar() -> HTToolBarView{
        return NSBundle.mainBundle().loadNibNamed("HTToolBarView", owner: nil, options: nil).last as! HTToolBarView
    }
    //设置第一个按钮选中
    override func willMoveToSuperview(newSuperview: UIView?) {
        super.willMoveToSuperview(newSuperview)
        toolBarselectBtn(0)
    }
    //根据 collection内容选定对应的按钮
    func toolBarselectBtn(section: Int){
        let button = self.subviews[section] as! UIButton
        switchSelectedBtn(button)
    }
    //点击按钮
    @IBAction func DidClickToolBarBtn(sender: UIButton) {
        Xprint(msg: "\(sender.tag)")
        print("\(sender.tag)")
        switchSelectedBtn(sender)
        delegate?.toolBar(self, selectedType: EmotiontoolBarButtonType(rawValue: sender.tag)!)
    }
    private func switchSelectedBtn(btn: UIButton){
        //上次和本次相同的话，就直接返回
        if self.selectedBtn == btn{
            return
        }
        //取消上一次选中按钮
        self.selectedBtn?.selected = false
        //本次按钮选中
        btn.selected = true
        //将本次设置为选中按钮
        self.selectedBtn = btn
    }


}
